<?php
/**
 *  @author Pham Van Diep
 *  @date: 18/2/2014
 *  @description: save update club
 */
// not login
// not perm ad
if(!isset($AppUI->id) || 
   !isset($_SESSION['club']) ||
   (isset($_SESSION['club']) && ($_SESSION['club']&1<<1)==0)
) {
    redirect(HOST_BACKEND);
}
// data input
$arrErr             = array();
$strName            = CI_Function::stripTag($this->input->post('name',null));
$intLen = strlen($strName); 
if($intLen < VL_NAME_MINLENGTH || $intLen > VL_NAME_MAXLENGTH ) {
	array_push($arrErr,$arrLocale['name_range']);
}
$strNickName        = CI_Function::stripTag($this->input->post('nickname',null)); 	
$intLen = strlen($strNickName);
if($intLen < VL_NAME_MINLENGTH || $intLen > VL_NAME_MAXLENGTH ) {
	array_push($arrErr,$arrLocale['nickname_range']);
}		
$strCountry         = CI_Function::stripTag($this->input->post('country',null));
$intLen = strlen($strCountry);
if($intLen != 2 ) {
	array_push($arrErr,$arrLocale['country_condition']	);
}
$strLogo            = CI_Function::stripTag($this->input->post('link',null));
$this->load->library('upload');
if(isset($strLink) && !empty($strLink)) {
    $strLogo = $this->upload->do_upload_url($strLink);
     $strLogo = $strLogo['path'];
}elseif( isset($_FILES['logo']['name']) && !empty($_FILES['logo']['name'])) {        
    $strLogo = $this->upload->do_upload('logo');    
    $strLogo = $strLogo['path'];
    $this->load->model('image_model','image');
} else {
    array_push($arrErr,'');
}
$strSlug            = CI_Function::stripTag($this->input->post('slug', null));
$strShort           = CI_Function::stripTag($this->input->post('sshort',null));
$strDescription     = CI_Function::stripTag($this->input->post('description',null));
$strTag             = CI_Function::stripTag($this->input->post('tag',null));



if(isset($arrErr) && count($arrErr) == 0) {//allow save   
	if(isset($_SESSION['CLUB_ERROR'])) unset($_SESSION['CLUB_ERROR']);
	if(isset($_SESSION['CLUB_INPUT'])) unset($_SESSION['CLUB_INPUT']);
    $ret = $this->club->add($AppUI->id,
    						$strName,
    						$strNickName,
    						$strCountry,
    						$strLogo,
    						$strSlug,
    						$strShort,
    						$strDescription,
    						$strTag);
    if($ret == 0) CI_Function::setMsg('Add sucessfully.'); 
    else if($ret == -1) array_push($arrErr, $arrError['system_error']);
    else array_push($arrErr,$arrError['problem_save_db_error']);       
}
// occur error

if (isset($arrErr) && count($arrErr) > 0) {
   $_SESSION['CLUB_INPUT'] = $this->input->post();  
   $_SESSION['CLUB_ERROR']=$arrErr;   
   redirect(HOST_BACKEND . 'club/add' );
}
redirect(isset($_SESSION[SESSION_BACKURL]) ? $_SESSION[SESSION_BACKURL] : HOST_BACKEND);
